package com.bosssoft.boss_exam_db.service.busines.impl;

import com.bosssoft.boss_exam_db.entity.Company;
import com.bosssoft.boss_exam_db.entity.Question;
import com.bosssoft.boss_exam_db.mapper.QuestionMapper;
import com.bosssoft.boss_exam_db.model.vo.GeneratePaperVO;
import com.bosssoft.boss_exam_db.model.vo.QuestionVO;
import com.bosssoft.boss_exam_db.service.base.impl.BaseServiceImpl;
import com.bosssoft.boss_exam_db.service.busines.QuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class QuestionServiceImpl extends BaseServiceImpl<Question> implements QuestionService {

    //注入QuestionMapper
    @Autowired
    private QuestionMapper questionMapper;


    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/16
     * @Description  根据题目名称模糊查询 或者根据题型查询
     * @Param [company, questionTypeId, questionName, page, limit, sort, order]
     * @Return com.github.pagehelper.PageInfo<com.bosssoft.boss_exam_db.entity.Question>
     * @Exception //TODO
     * @since JDK 1.8
     **/
    @Override
    public PageInfo<QuestionVO> queryByCondition(Company company, String questionTypeId, String questionName, Integer page, Integer limit, String sort, String order) {
      PageHelper.startPage(page, limit).setOrderBy("question."+sort + " " + order);
        List<QuestionVO> questionVOS = this.questionMapper.queryByCondition(company.getId(), questionTypeId, questionName);
        PageInfo<QuestionVO> questionVOPageInfo = new PageInfo<>(questionVOS);
        return questionVOPageInfo;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/19
     * @Description 查询符合条件的题目的ID列表
     * @Param [generatePaperVO]
     * @Return java.util.List<java.lang.String>
     * @Exception //无
     * @since JDK 1.8
     **/
    @Override
    public List<String> queryQuestionForGreneratePaper(Company company,GeneratePaperVO generatePaperVO) {
        Example example = new Example(Question.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(company.getId())){
            criteria.andEqualTo("companyId",company.getId());
        }
        if(!StringUtils.isEmpty(generatePaperVO.getSubjectId())){
            criteria.andEqualTo("subjectId",generatePaperVO.getSubjectId());
        }
        if(!StringUtils.isEmpty(generatePaperVO.getQuestionTypeId())){
            criteria.andEqualTo("questionTypeId",generatePaperVO.getQuestionTypeId());
        }
        if(!StringUtils.isEmpty(generatePaperVO.getQuestionLevel())){
            criteria.andEqualTo("questionLevel",generatePaperVO.getQuestionLevel());
        }
        criteria.andEqualTo("deleted",false);
        List<Question> questions = questionMapper.selectByExample(example);
        List<String> idList=null;
        if(questions!=null){
            idList= questions.stream().map(question -> question.getId()).collect(Collectors.toList());
        }
        return idList;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/19
     * @Description 根据ID列表查询问题集合
     * @Param [selectIds]
     * @Return java.util.List<com.bosssoft.boss_exam_db.entity.Question>
     * @Exception //TODO
     * @since JDK 1.8
     **/
    @Override
    public List<Question> queryQuestionBySomeIds(ArrayList<String> selectIds) {
        Example example = new Example(Question.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id",selectIds)
                .andEqualTo("deleted",false);
        List<Question> questions = questionMapper.selectByExample(example);
        return questions;
    }



}
